﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using IKEA.FileExchange.Domain;
using IKEA.FileExchange.Domain.Files;
using IKEA.FileExchange.Domain.FilePackage;
using System.Collections.Generic;

public partial class controls_DownloadTicketViewControl : System.Web.UI.UserControl
{
    public class FileItem
    {
        public string FileName;
        public string FileSize;
        public string FileDeletionDate;
        public string FileUniqueId;
        public string FileUrl;
        public string DownloadUrl;
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        string downloadTicketIdStr = Request.QueryString["dt"];

        Guid downloadTicketId = new Guid(downloadTicketIdStr);

        PopulateControls(downloadTicketId);
    }

    protected void PopulateControls(Guid downloadTicketId)
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite spSite = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb spWeb = spSite.OpenWeb())
                {
                    SPList ticketList = IKEAFileStoreHelper.GetDownloadTicketsList(spWeb);

                    SPListItem ticket = ticketList.GetItemByUniqueId(downloadTicketId);

                    if (ticket != null)
                    {
                        string packageIdStr = ticket[FileFieldNames.FilePackageReference].ToString().Split(';')[0];

                        int packageId = int.Parse(packageIdStr);

                        SPList packageItemList = IKEAFileStoreHelper.GetFilePackagesList(spWeb);

                        SPListItem packageItem = packageItemList.GetItemById(packageId);
                        FilePackageInformation packageInformation = FilePackageInformationProvider.Extract(packageItem);

                        txtUploadComments.Text = packageInformation.UploadComments;

                        SPQuery q = new SPQuery();

                        SPFieldLookupValue lookupValue = new SPFieldLookupValue(packageItem.ID, packageItem.Title);

                        string queryText = string.Format("<Where><Eq><FieldRef Name='{0}'/><Value Type='Text'>{1}</Value></Eq></Where>",
                            "IKEAFilePackageReference", lookupValue.LookupValue);



                        q.Query = queryText;
                        q.ViewAttributes = "Scope=\"Recursive\"";


                        SPList fileStore = null;

                        if (packageInformation.IsConfidential)
                        {
                            fileStore = IKEAFileStoreHelper.GetConfidentialFileStore(spWeb);
                        }
                        else
                        {
                            fileStore = IKEAFileStoreHelper.GetPublicFileStore(spWeb);
                        }

                        SPListItemCollection files = fileStore.GetItems(q);
                        List<FileItem> filesDataSource = new List<FileItem>();

                        foreach (SPListItem file in files)
                        {
                            FileItem fi = new FileItem();

                            filesDataSource.Add(fi);

                            fi.FileDeletionDate = file[FileFieldNames.FileDeletionDate].ToString();
                            fi.FileName = file.File.Name;
                            fi.FileUrl = Page.ResolveClientUrl("~/_layouts/images/" + file.File.IconUrl);

                            int fileSizeInBytes = int.Parse(file["File Size"].ToString());
                            fi.FileSize = ((long)(fileSizeInBytes / (1024))).ToString() + " KB";

                            fi.FileUniqueId = file.UniqueId.ToString();

                            string downloadUrl = string.Format("~/_layouts/FileExchangeLayouts/services/DownloadTicketProcessingService.aspx?t={0}&f={1}&p={2}&nc={3}",
                                ticket.UniqueId, file.UniqueId.ToString(), packageItem.UniqueId.ToString(),DateTime.Now.Ticks.ToString());
                            fi.DownloadUrl = Page.ResolveClientUrl(downloadUrl);
                        }

                        gvFiles.DataSource = filesDataSource;
                        gvFiles.DataBind();
                    }
                }
            }
        }
        );
    }
}
